package org.example.utils.oaa;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.example.dao.dao.market.CStoreDao;
import org.example.dao.dto.market.BusinessDto;
import org.example.dao.entity.market.CStore;
import org.example.dao.entity.user.AuPermission;
import org.example.dao.entity.user.AuRole;
import org.example.skytool.commom.ContextUtil;
import org.example.skytool.commom.IdWorker;
import org.example.skytool.commom.StringUtil;
import org.example.skytool.json.JackSonUtil;
import org.example.skytool.redis.RedisUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

import java.util.*;
import java.util.concurrent.TimeUnit;

import static org.example.skytool.redis.RedisConstants.*;

/**
 * @author xluo
 * @date 2023/10/30 14:29
 */
@Component
public class RedisBusinessService {

    @Resource
    private RedisUtils redisUtils;

    private String getCacheTokenKey(String businessId) {
        String key = USER_MOUDLE + businessId + USER_OAA_CONTROLLER + USER_OAA_JWT_KIND;
        return key;
    }

    private String getBusinessDtoKey(String businessId) {
        String key = MARKET_MOUDLE + ":" + businessId + MARKET_DATA_CONTROLLER + MARKET_DATA_DTO_CONTROLLER;
        return key;
    }

    private String getCachePermissionKey(String businessId) {
        String key = USER_MOUDLE + businessId + USER_OAA_CONTROLLER + USER_OAA_PERMISSION_KIND;
        return key;
    }

    private String getCacheRoleKey(String businessId) {
        String key = USER_MOUDLE + businessId + USER_OAA_CONTROLLER + USER_OAA_ROLE_KIND;
        return key;
    }

    public void setCacheToken(String businessId, String token) {
        redisUtils.stringSet(getCacheTokenKey(businessId), token);
    }

    public String getCacheToken(String businessId) {
        return redisUtils.stringGet(getCacheTokenKey(businessId));
    }

    public void delCacheToken(String businessId) {
        redisUtils.del(getCacheTokenKey(businessId));
    }

    public void setBusinessDto(BusinessDto businessDto) {
        String userId = businessDto.getBusiness().getId().toString();
        redisUtils.objectSet(getBusinessDtoKey(userId), businessDto, 30L, TimeUnit.MINUTES);
    }

    public void setCachePermission(BusinessDto businessDto) {
        String id = businessDto.getBusiness().getId().toString();
        Set<String> roles = new HashSet<>();
        Set<String> permissions = new HashSet<>();
        for(AuRole role : businessDto.getRoleList()){
            roles.add(role.getName());
        }
        for(AuPermission permission : businessDto.getPermissionList()){
            permissions.add(permission.getName());
        }
        if (roles.size() != 0) {
            redisUtils.setAdd(getCacheRoleKey(id), roles);
        }
        if (permissions.size() != 0) {
            redisUtils.setAdd(getCachePermissionKey(id), permissions);
        }
    }

}
